﻿using System.Collections.Generic;
using DataScripter.Database;
using DataScripter.Scripting.Compare.InsideSqlServer.Data;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;

namespace DataScripter.UnitTests.Processes.Compare.Data.Insert
{
    [TestClass]
    public class WhenInsertingRecords
    {
        [TestMethod]
        public void TestSql()
        {
            Mock<IDatabaseStructure> leftDatabase = new Mock<IDatabaseStructure>(MockBehavior.Strict);
            leftDatabase.Setup(ds => ds.Database).Returns("LeftDB");
            Mock<IDatabaseStructure> rightDatabase = new Mock<IDatabaseStructure>(MockBehavior.Strict);
            rightDatabase.Setup(ds => ds.Database).Returns("RightDB");
            Mock<ITableStructure> leftTable = new Mock<ITableStructure>(MockBehavior.Strict);
            leftTable.Setup(ts => ts.PrimaryKeyFields).Returns(new List<FieldInfo> {new FieldInfo("LeftId", "UNIQUEIDENTIFIER")});
            leftTable.Setup(ts => ts.SchemaName).Returns("dbo");
            leftTable.Setup(ts => ts.TableName).Returns("leftTable");
            Mock<ITableStructure> rightTable = new Mock<ITableStructure>(MockBehavior.Strict);
            rightTable.Setup(ts => ts.SchemaName).Returns("dbo");
            rightTable.Setup(ts => ts.TableName).Returns("rightTable");
            Mock<Db> dbMock = new Mock<Db>();
            RecordsInserter inserter = new RecordsInserter(dbMock.Object, leftDatabase.Object, leftTable.Object, rightDatabase.Object, rightTable.Object);
            leftDatabase.VerifyAll();
            rightDatabase.VerifyAll();
            leftTable.VerifyAll();
            rightTable.VerifyAll();
            Assert.AreEqual("INSERT INTO RightDB.dbo.rightTable SELECT [A].* FROM LeftDB.dbo.leftTable A INNER JOIN RightDB.dbo.rightTable B ON A.LeftId=B.LeftId ORDER BY [A].LeftId", inserter.Sql);
        }
    }
}
